/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package createinst;

import edu.ipfw.cs590.cheat.AccessRights;
import simpleclient.*;
//import edu.ipfw.cs590.cheat.AccessType;
import edu.ipfw.cs590.cheat.DenyRegistrationRequest;
import edu.ipfw.cs590.cheat.DenyRegistrationRequestResponse;
import edu.ipfw.cs590.cheat.GetAccessRights;
import edu.ipfw.cs590.cheat.GetAccessRightsResponse;
import edu.ipfw.cs590.cheat.GetPendingRegistrationRequests;
import edu.ipfw.cs590.cheat.GetPendingRegistrationRequestsResponse;
import edu.ipfw.cs590.cheat.GrantRegistrationRequest;
import edu.ipfw.cs590.cheat.GrantRegistrationRequestResponse;
import edu.ipfw.cs590.cheat.Logout;
import edu.ipfw.cs590.cheat.LogoutResponse;
//import edu.ipfw.cs590.cheat.Person;
//import edu.ipfw.cs590.cheat.RegisteredUser;
import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.RegisteredPerson;
import edu.ipfw.cs590.cheat.UserRights;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

/**
 *
 * @author rrm46582
 */
public class Main {
    static List<RegisteredPerson> pendingUsers;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        try
        {
            // TODO code application logic here
            callRegistration("x@x.com");
            int sessId = 214;
            callGetAccessRights(sessId);
            callGetPendingRequests(sessId);
            callGrant(sessId, "x@x.com");
        } 
        catch (MalformedURLException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }




 private static void callGrant(int sessId, String user)
 {
    URL wsdlURL = null;
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


            GrantRegistrationRequest deny = new GrantRegistrationRequest();

            deny.setPendingUser(user);
            GrantRegistrationRequestResponse answer = jiPort.grantRegistrationRequest(deny, new Integer(sessId).toString());

            System.out.println("Access for:" + user + " " + answer.isGrantResult());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
 }




  private static void callGetAccessRights(int sessId) throws MalformedURLException
  {

    URL wsdlURL = null;
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


			GetAccessRights ar = new GetAccessRights();
            GetAccessRightsResponse answer = jiPort.getAccessRights(ar, new Integer(sessId).toString());

            System.out.println("Access: " + answer.getAccessRightsResult().getRights());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
	}


  private static void callGetPendingRequests(int sessId) throws MalformedURLException
  {

    URL wsdlURL = null;
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


			GetPendingRegistrationRequests ar = new GetPendingRegistrationRequests();
            GetPendingRegistrationRequestsResponse answer = jiPort.getPendingRegistrationRequests(ar, new Integer(sessId).toString());

            pendingUsers = answer.getGetPendingResult();
            System.out.println("Pending Users:");
            for (int i = 0; i < pendingUsers.size(); i++)
            {
                System.out.println("\t" + pendingUsers.get(i).getEmail());
            }
            System.out.println("_____________________");

		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
	}



   private static void callRegistration(String user) throws MalformedURLException
    {

        URL wsdlURL = null;

		try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


            RegisteredPerson r = new RegisteredPerson();
//            Person p = new Person();
            UserRights userRights = UserRights.ADMINISTRATOR;
            AccessRights accessRights = new AccessRights();
            accessRights.setRights(userRights);
            r.setAffiliation("The University of Relative Truth");
            r.setEmail(user);
            r.setFirstName("Sarcastic");
            r.setLastName("Bastard");
            r.setPhone("999.666.6669");
            r.setUrl("www.dotheworngthing.always");
            r.setRights(accessRights);
            r.setPassword("luck");
            r.setUserId(user);

            boolean answer = jiPort.register(r);
            System.out.println(answer);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}

    }
}
